#include "timerbtn.h"
#include <QMouseEvent>

TimerBtn::TimerBtn(QWidget *parent)
    :QPushButton(parent),counter_(10)
{
    timer_ = new QTimer(this);
    //连接信号和槽
    connect(timer_,&QTimer::timeout,this,[this](){
        //查看counter_是否到0了
        counter_--;
        if(counter_<=0)
        {
            timer_->stop();
            counter_=0;
            this->setText("获取");
            //将button重新设置为可点击的
            this->setEnabled(true);
            return;
        }
        this->setText(QString::number(counter_));
    });
}

TimerBtn::~TimerBtn()
{
    timer_->stop();
}

void TimerBtn::mouseReleaseEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        qDebug()<<"MyButton was released\n";
        this->setEnabled(false);
        timer_->start(1000);

        this->setText(QString::number(counter_));
        emit clicked();
    }
    QPushButton::mouseMoveEvent(e);
}
